from flask import Flask, render_template,request, url_for
import pandas as pd
import matplotlib.pyplot as plt
import plotly as py
import plotly.graph_objs as go
import cufflinks as cf

app = Flask(__name__)

df = pd.read_csv('hurun.csv', encoding='utf-8', delimiter="\t")
regions_available = list(df.region.dropna().unique())
cf.set_config_file(offline=True, theme="ggplot")
py.offline.init_notebook_mode()

@app.route("/get_hurun_info")
def hurun():
  df = pd.read_csv('hurun.csv', encoding='utf-8', delimiter="\t")
  return render_template(
    "hurun_info.html",
    df_表格 = df.to_html(classes='animation',index=False),
    data = [{'环杭州湾大湾区情况':'A'},{'渤海大湾区情况':'B'},{'粤港澳大湾区情况':'C'},{'三大湾区行业对比':'D'},{'三大湾区产业估值总和对比':'E'}])

@app.route("/picture",methods=['GET','POST'])
def picture():
  select = request.form.get('comp_select')
  df = pd.read_csv('hurun.csv', encoding='utf-8', delimiter="\t")
  df_环杭州湾大湾区 = df[df['region'].str.contains('环杭州湾大湾区')]
  df_渤海大湾区 = df[df['region'].str.contains('渤海大湾区')]
  df_粤港澳大湾区 = df[df['region'].str.contains('粤港澳大湾区')]
  df_环杭州湾大湾区产业估值合计 = df_环杭州湾大湾区.groupby('行业').sum().drop(['成立年份'],axis=1).reset_index()
  df_渤海大湾区产业估值合计 = df_渤海大湾区.groupby('行业').sum().drop(['成立年份'],axis=1).reset_index()
  df_粤港澳大湾区产业估值合计 = df_粤港澳大湾区.groupby('行业').sum().drop(['成立年份'],axis=1).reset_index()
  fig = df_环杭州湾大湾区产业估值合计.iplot(kind="bar", x="行业", y="估值（亿人民币）", asFigure=True,title='环杭州湾大湾区产业估值合计')
  py.offline.plot(fig, filename="环杭州湾大湾区产业估值合计.html",auto_open=False)
  with open("环杭州湾大湾区产业估值合计.html", encoding="utf8", mode="r") as f:
    plot_all = "".join(f.readlines())
  fig = df_渤海大湾区产业估值合计.iplot(kind="bar", x="行业", y="估值（亿人民币）", asFigure=True,title='渤海大湾区产业估值合计')
  py.offline.plot(fig, filename="渤海大湾区产业估值合计.html",auto_open=False)
  with open("渤海大湾区产业估值合计.html", encoding="utf8", mode="r") as f:
    plot_all2 = "".join(f.readlines())
  fig = df_粤港澳大湾区产业估值合计.iplot(kind="bar", x="行业", y="估值（亿人民币）", asFigure=True,title='粤港澳大湾区产业估值合计')
  py.offline.plot(fig, filename="粤港澳大湾区产业估值合计.html",auto_open=False)
  with open("粤港澳大湾区产业估值合计.html", encoding="utf8", mode="r") as f:
    plot_all3 = "".join(f.readlines())
  df_环杭州湾大湾区产业估值总和 = df_环杭州湾大湾区['估值（亿人民币）'].sum()
  df_渤海大湾区产业估值总和 = df_渤海大湾区['估值（亿人民币）'].sum()
  df_粤港澳大湾区产业估值总和 = df_粤港澳大湾区['估值（亿人民币）'].sum()
  df_环杭州湾大湾区产业均值 = df_环杭州湾大湾区['估值（亿人民币）'].mean()
  df_渤海大湾区产业均值 = df_渤海大湾区['估值（亿人民币）'].mean()
  df_粤港澳大湾区产业均值 = df_粤港澳大湾区['估值（亿人民币）'].mean()
  df_all = pd.DataFrame({'指标':['粤港澳大湾区产业估值总和','粤港澳大湾区产业均值','渤海大湾区产业估值总和','渤海大湾区产业均值','环杭州湾大湾区产业估值总和','环杭州湾大湾区产业均值'],'值':[df_粤港澳大湾区['估值（亿人民币）'].sum(),df_粤港澳大湾区['估值（亿人民币）'].mean(),df_渤海大湾区['估值（亿人民币）'].sum(),df_渤海大湾区['估值（亿人民币）'].mean(),df_环杭州湾大湾区['估值（亿人民币）'].sum(),df_环杭州湾大湾区['估值（亿人民币）'].mean()]})
  fig = df_all.iplot(kind="bar", x="指标", y="值", asFigure=True,title='三大湾区产业指标对比')
  py.offline.plot(fig, filename="example.html",auto_open=False)
  with open("example.html", encoding="utf8", mode="r") as f:
    plot_all4 = "".join(f.readlines())
  return render_template(
    "info.html",
    select = select,
    df_环杭州湾大湾区 = df_环杭州湾大湾区.to_html(classes='animation',index=False),
    df_渤海大湾区 = df_渤海大湾区.to_html(classes='animation',index=False),
    df_粤港澳大湾区 = df_粤港澳大湾区.to_html(classes='animation',index=False),
    df_all = df_all.to_html(classes='animation',index=False),
    df_渤海大湾区产业估值合计 = df_渤海大湾区产业估值合计.to_html(classes='animation',index=False),
    df_环杭州湾大湾区产业估值合计 = df_渤海大湾区产业估值合计.to_html(classes='animation',index=False),
    df_粤港澳大湾区产业估值合计 = df_粤港澳大湾区产业估值合计.to_html(classes='animation',index=False),
    grid = plot_all,
    grid2 = plot_all2,
    grid3 = plot_all3,
    grid4 = plot_all4,)




if __name__ == '__main__':
    app.run(debug=True)

